package com.example.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.delegator.CustomEXADefinitionDelegator;
import com.example.result.ReturnCode;
import com.example.result.ReturnResult;
import com.example.service.IAttributeManagementService;
import com.example.vo.AttributeCreateFormVO;
import com.example.vo.AttributeQueryViewVO;
import com.example.vo.AttributeUpdateFormVO;
import com.huawei.innovation.rdm.coresdk.basic.dto.PersistObjectIdDecryptDTO;
import com.huawei.innovation.rdm.coresdk.basic.dto.PersistObjectIdsModifierDTO;
import com.huawei.innovation.rdm.coresdk.basic.enums.ConditionType;
import com.huawei.innovation.rdm.coresdk.basic.vo.QueryCondition;
import com.huawei.innovation.rdm.coresdk.basic.vo.QueryRequestVo;
import com.huawei.innovation.rdm.coresdk.basic.vo.RDMPageVO;
import com.huawei.innovation.rdm.xdm.delegator.EXADefinitionDelegator;
import com.huawei.innovation.rdm.xdm.dto.entity.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AttributeManagementService implements IAttributeManagementService {
    @Autowired
    private EXADefinitionDelegator exaDefinitionDelegator;
    @Autowired
    private CustomEXADefinitionDelegator customEXADefinitionDelegator;
    @Override
    public List<AttributeQueryViewVO> queryAttributeByName(String name) {
        QueryRequestVo queryRequestVo = new QueryRequestVo();
        QueryCondition queryCondition = new QueryCondition();
        if(name!=null) {
            queryCondition.setJoiner("or");
            queryCondition.addCondition("nameEn", ConditionType.LIKE, name);
            queryCondition.addCondition("name", ConditionType.LIKE, name);
        }
        queryRequestVo.setFilter(queryCondition);
        List<EXADefinitionQueryViewDTO> query = exaDefinitionDelegator.query(queryRequestVo, new RDMPageVO());
        List<AttributeQueryViewVO> newlist = new ArrayList<>();
        for (EXADefinitionQueryViewDTO e :
                query) {
            AttributeQueryViewVO vo = new AttributeQueryViewVO();
            String[] ignoreProperties = new String[]{"folder","id"};
            BeanUtils.copyProperties(e, vo,ignoreProperties);
            vo.setId(String.valueOf(e.getId()));
            //过滤目录的敏感属性
            AttributeQueryViewVO.CustomFolder folder = new AttributeQueryViewVO.CustomFolder();
            FolderQueryViewDTO eFolder = e.getFolder();
            folder.setName(eFolder.getName());
            folder.setNameEn(eFolder.getNameEn());
            folder.setBusinessCode(eFolder.getBusinessCode());
            vo.setFolder(folder);
            newlist.add(vo);
        }
        return newlist;
    }

    @Override
    public ReturnResult create(AttributeCreateFormVO createForm) {
        EXADefinitionCreateDTO dto = new EXADefinitionCreateDTO();
        dto.setName(createForm.getName());
        dto.setNameEn(createForm.getNameEn());
        dto.setCategory(createForm.getCategory());
        dto.setType(createForm.getType());
        dto.setDescription(createForm.getDescription());
        dto.setDescriptionEn(createForm.getDescriptionEn());
        dto.setDisableFlag(createForm.getDisableFlag());
        
        //此处硬编码
        String dtoType = dto.getType();
        JSONObject jsonObject = new JSONObject();
        if(dtoType.equals("STRING")){
            jsonObject.put("length","255");
        } else if (dtoType.equals("DECIMAL")) {
            jsonObject.put("precision","9");
        }else {
            jsonObject.put("notnull",false);//我随便设一个的，不弄会报错
        }
        dto.setConstraint(jsonObject.toJSONString());
        EXADefinitionViewDTO exaDefinitionViewDTO = exaDefinitionDelegator.create(dto);
        return new ReturnResult(ReturnCode.INSERT_OK,"成功创建属性",exaDefinitionViewDTO);
    }

    @Override
    public ReturnResult update(AttributeUpdateFormVO formVO) {
//        Object o = customEXADefinitionDelegator.updateAttribute(formVO);
        Long id = formVO.getId();
        //根据属性id 查询 属性细节
        PersistObjectIdDecryptDTO dto1 = new PersistObjectIdDecryptDTO();
        dto1.setId(id);
        EXADefinitionViewDTO exaDefinitionViewDTO = exaDefinitionDelegator.get(dto1);
        String dtoType = exaDefinitionViewDTO.getType();
        String constraint = exaDefinitionViewDTO.getConstraint();
        //制作更新表单
        EXADefinitionUpdateDTO dto2 = new EXADefinitionUpdateDTO();
        dto2.setId(id);
        dto2.setType(dtoType);
        dto2.setConstraint(constraint);
        dto2.setDescription(formVO.getDescription());
        dto2.setDescriptionEn(formVO.getDescriptionEn());
        //请求更新
        EXADefinitionViewDTO viewDTO = exaDefinitionDelegator.update(dto2);

        return new ReturnResult(ReturnCode.UPDATE_OK,"修改成功",viewDTO);
    }

    @Override
    public ReturnResult batchDeleteByIds(List<Long> ids) {
        PersistObjectIdsModifierDTO dto = new PersistObjectIdsModifierDTO();
        dto.setIds(ids);
        int i = exaDefinitionDelegator.batchDelete(dto);
        return new ReturnResult(ReturnCode.DELETE_OK,"删除成功",i);
    }
}
